package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.DefaultGroupBean;
import com.xuxin.nantai.bean.FlowLineBean;
import com.xuxin.nantai.bean.GroupBean;
import com.xuxin.nantai.bean.ProducerBean;
import com.xuxin.nantai.contract.CompleteDetailUpdateContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.CompleteDetailUpdatePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.09 0009
 */
public class CompleteDetailUpdateModel implements CompleteDetailUpdateContract.Model {
    @Override
    public void getGroups(CompleteDetailUpdatePresenter presenter) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getGroups()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<GroupBean>() {
            @Override
            public void onNext(GroupBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getFlowLine(CompleteDetailUpdatePresenter presenter) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getFlowLine()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<FlowLineBean>() {
            @Override
            public void onNext(FlowLineBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getProducer(CompleteDetailUpdatePresenter presenter, String cDepCode, String LineName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getProducer(cDepCode, LineName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<ProducerBean>() {
            @Override
            public void onNext(ProducerBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }


    @Override
    public void update(CompleteDetailUpdatePresenter presenter,
                       String LoginName,
                       String UserName,
                       String Guid,
                       String DID,
                       String HID,
                       String GroupName,
                       String GroupCode,
                       String ProductName,
                       String ProductCode,
                       String DefectDes,
                       String DefectNum) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .update(LoginName,
                        UserName,
                        Guid,
                        DID,
                        HID,
                        GroupName,
                        GroupCode,
                        ProductName,
                        ProductCode,
                        DefectDes,
                        DefectNum)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getDefaultGroup(CompleteDetailUpdatePresenter presenter, String LoginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getDefaultGroup(LoginName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<DefaultGroupBean>() {
            @Override
            public void onNext(DefaultGroupBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}